<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商管理平台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>

      <!-- 侧边菜单栏 -->
      <el-aside :width="isCollapse?'40px':'200px'">
        <div class="toggle" @click="isCollapse=!isCollapse">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened="true"
          :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单循环 -->
          <el-submenu :index='obj.id+"1"' v-for="obj of menuList" :key="obj.id">
            <template slot="title">
              <i :class="iconsObj[obj.id]"></i>
              <span style="margin-left:4px">{{obj.authName}}</span>
            </template>
            <!-- 二级菜单循环 -->
            <el-menu-item :index="'/'+item.path" v-for="item in obj.children" :key="item.id"
              @click="changActivePath('/'+item.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 路由占位 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    changActivePath (path) {
      this.activePath = path
      window.sessionStorage.setItem('activePath', path)
    },
    logout () {
      window.sessionStorage.clear('token')
      this.$router.push('/login')
      this.$message.error('退出登录成功')
    },
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(this.menuList)
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
  }
  .el-menu {
    border-right: none;
  }
  // .el-menu-item {
  //   box-sizing: border-box;
  // }
  .toggle {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
